package com.eryos.gwt.mailmodule.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Mail_module implements EntryPoint {

	/**
	 * Create a remote service proxy to talk to the server-side service.
	 */
	private final ReCaptchaServiceAsync captchaService = GWT.create(ReCaptchaService.class);
	private final MailServiceAsync mailService = GWT.create(MailService.class);

	private Element statusElement;

	public void onModuleLoad(){
		MailPresenter presenter = new MailPresenter(mailService, new MailView());
		presenter.go(RootPanel.get("formContainer"));
	}
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoadOld() {

		final String PUBLIC_KEY    	= "6Ld5OskSAAAAAAdwlooG9nm_b7tsVNH0Gr8DhR2S";
		final String CAPTCHA_DIV_ID 	= "captchaContainer";
		final String BUTTON_DIV_ID  	= "buttonContainer";
		final String STATUS_DIV_ID  	= "statusContainer";

		createCaptcha(PUBLIC_KEY, CAPTCHA_DIV_ID, "white");

		Button validCaptchaButton = new Button("Valid Captcha");
		RootPanel.get(BUTTON_DIV_ID).add(validCaptchaButton);

		statusElement = DOM.getElementById(STATUS_DIV_ID);

		validCaptchaButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {

				logCaptcha("Waiting .. check in progress ...");

				String challenge = getCaptchaChallenge();
				String response = getCaptchaResponse();

				logCaptcha("Challenge : "+challenge);
				logCaptcha("Response : "+response);

				captchaService.checkCaptach(challenge, response, new AsyncCallback<Boolean>(){

					@Override
					public void onFailure(Throwable caught) {
						logCaptcha("!!! Error while checking captcha : <b>"+caught.getMessage()+"</b> !!!");
					}

					@Override
					public void onSuccess(Boolean result) {
						if ( result ){
							// CAPTCHA OK
							logCaptcha("Captcha OK ... congratulation ...");
						}else {
							// CAPTCHA NOK
							logCaptcha("Captcha ERROR ... try again ...");
						}
					}
				});

			}
		});

	}

	private void logCaptcha(String msg){
		if ( statusElement != null ) 
			statusElement.setInnerText(statusElement.getInnerText()+"<br/>"+msg);
	}

	/**
	 * Used to instantiate the captcha in the page
	 */
	public static native void createCaptcha(String publicKey, String captchaDivId, String themeColor) /*-{
		$wnd.Recaptcha.create(publicKey, captchaDivId, {
		theme: themeColor,
		callback: $wnd.Recaptcha.focus_response_field
		});
	}-*/; 

	/**
	 * Retrieve the reCaptcha response value set by the user
	 * @return
	 */
	public static native String getCaptchaResponse() /*-{
		return $wnd.Recaptcha.get_response();
	}-*/; 

	/**
	 * Retrieve a string representation of reCaptcha Challenge
	 * @return
	 */
	public static native String getCaptchaChallenge() /*-{
		return $wnd.Recaptcha.get_challenge()
	}-*/; 
}
